<template>
  <div class="app-container">
    <keep-alive>
    </keep-alive>
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="批次名称：" prop="name" required>
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="批次人员：" prop="studentArray" required>
        <el-select v-model="form.studentArray" multiple collapse-tags  placeholder="批次人员">
    <el-option
      v-for="item in students "
      :key="item.id"
      :label="item.userName"
      :value="item.id">
    </el-option>
  </el-select>
      </el-form-item>
      <el-form-item label="测试：" prop="taskId" required="">
      <el-select v-model="form.taskId" placeholder="测试" >
          <!-- <el-checkbox v-model="checked" @change='selectAll'>全选</el-checkbox> -->
          <el-option v-for="item in taskArray " :key="item.id" :value="item.id" :label="item.title"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import studentApi from '@/api/student'
import subjectApi from '@/api/subject'
import taskApi from '@/api/task'
import { mapActions, mapGetters, mapState } from 'vuex'

export default {
  data () {
    return {
      form: {
        id: '',
        name: '',
        taskId: '',
        studentArray: []
        // level: 1,
        // levelName: ''
      },
      checked: false,
      selectedArr: [],
      formLoading: false,
      students: [],
      taskArray: [],
      rules: {
        name: [
          { required: true, message: '请填写批次名称', trigger: 'blur' }
        ],
        studentArray: [
          { required: true, message: '请选择批次人员', trigger: 'change' }
        ],
        taskId: [
          { required: true, message: '请选择测试的试卷', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    // 选项
    selectOptions: {
      type: Array,
      default () {
        return []
      }
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 已选中选项
    mulSelecteds: {
      type: Array,
      default () {
        return []
      }
    },
    mulSelectLoading: {
      type: Boolean,
      default: false
    },
    collapseTags: {
      type: Boolean,
      default: true
    }
  },
  created () {
    let id = this.$route.query.id
    this.initStudent()
    this.initTask()
    let _this = this
    if (id) {
      _this.formLoading = true
      subjectApi.select(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
    this.initTask(id)
  },
  watch: {
    mulSelecteds: {
      handler (newVal, oldVal) {
        this.selectedArr = newVal
        if (this.selectedArr.length &&
            this.selectedArr.length === this.selectOptions.length) {
          this.checked = true
        }
      },
      immediate: true
    }
  },
  selectAll () {
    this.selectedArr = []
    if (this.checked) {
      this.selectOptions.forEach((item) => { this.selectedArr.push(item.id) })
    } else {
      this.selectedArr = []
    }
    this.$emit('update:updateMultipleSelect', this.selectedArr)
  },
  changeSelect (val) {
    if (val.length === this.selectOptions.length) {
      this.checked = true
    } else {
      this.checked = false
    }
    this.$emit('update:updateMultipleSelect', this.selectedArr)
  },
  methods: {
    initStudent () {
      studentApi.getStudentList().then(data => {
        // eslint-disable-next-line eqeqeq
        if (data.code == 1) {
          this.students = data.response
        }
      })
    },
    initTask (id) {
      // if(id==null || id==undefined){
      //   id=-1
      // }
      taskApi.taskList(id).then(data => {
        this.taskArray = data.response
      })
    },
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          subjectApi.edit(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/education/subject/list')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        }
      })
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        name: ''
        // level: 1,
        // levelName: ''
      }
      this.form.id = lastId
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      levelEnum: state => state.user.levelEnum
    })
  }
}
</script>
